package pkg

import (
	"errors"
	"github.com/patrickmn/go-cache"
	"time"
)

// CacheClient 修改缓存客户端类型
type CacheClient struct {
	client *cache.Cache // 切换为 go-cache 类型
}

// NewCacheClient 调整初始化参数（保持原有过期逻辑）
func NewCacheClient() *CacheClient {
	return &CacheClient{
		client: cache.New(5*time.Minute, 10*time.Minute),
	}
}

// Set 方法适配新库（参数保持不变）
func (c *CacheClient) Set(key string, value interface{}, expiration time.Duration) error {
	if key == "" {
		return errors.New("cache key cannot be empty")
	}
	c.client.Set(key, value, expiration)
	return nil
}

// Get 方法需要调整类型断言方式
func (c *CacheClient) Get(key string) (string, error) {
	if key == "" {
		return "", errors.New("cache key cannot be empty")
	}

	value, found := c.client.Get(key)
	if !found {
		return "", errors.New("cache key not found")
	}

	strValue, ok := value.(string) // 保持类型断言
	if !ok {
		return "", errors.New("invalid cache value type")
	}
	return strValue, nil
}

// SetExpiration 方法需要新库实现
func (c *CacheClient) SetExpiration(key string, expiration time.Duration) error {
	if key == "" {
		return errors.New("cache key cannot be empty")
	}

	item, exists := c.client.Get(key)
	if !exists {
		return errors.New("key does not exist")
	}

	// 使用 go-cache 的 Set 方法更新过期时间
	c.client.Set(key, item, expiration)
	return nil
}
